package com.z_talk.server.model;

import com.z_talk.core.model.UserInfo;
import com.z_talk.core.model.UserModel;


import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.List;

public class OnlineUserTableModel extends AbstractTableModel {

    private static final long serialVersionUID = 1137742738869256013L;

    public OnlineUserTableModel(List<UserModel> onlineUsers) {
        onlineUsers.forEach(item -> {
            UserInfo userInfo = item.getUserInfo();
            String[] row = new String[]{userInfo.getUserId(),userInfo.getNickName(),userInfo.getPassword(),userInfo.getAge(),userInfo.getSex()};
            int rowSize = rows.size();
            rows.add(row);
            fireTableRowsInserted(rowSize, rowSize);
        });
    }

    /** 列名标题 */
    private String[] title = {"账号","昵称", "密码", "年龄", "性别"};
    /** 数据行 */
    private List<String[]> rows = new ArrayList<String[]>();

    @Override
    public int getRowCount() {
        return rows.size();
    }

    @Override
    public int getColumnCount() {
        return title.length;
    }
    @Override
    public String getColumnName(int column) {
        return title[column];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return (rows.get(rowIndex))[columnIndex];
    }

}
